package com.example.demo2.service;

import com.example.demo2.entity.Comment;
import com.example.demo2.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;


@Service

public class CommentService {
    @Autowired
    private CommentRepository commentRepository;


    public Comment findById(Integer id) {
        Optional<Comment> optionalComment = commentRepository.findById(id);
        return optionalComment.orElse(null);
    }


    public Comment update(Comment comment){
        Optional<Comment> optionalComment = commentRepository.findById(comment.getId());
        if (optionalComment.isPresent()){
            commentRepository.save(comment);
            return comment;
        }
        else {
            return null;
        }
    }


    public Comment insert(Comment comment){
        comment.setId(null);
        commentRepository.save(comment);
        return comment;
    }


    public void deleteById(Integer id){
        commentRepository.deleteById(id);
    }

}
